﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESBasic;
using ESBasic.ObjectManagement.Forms;
using ESPlus.Rapid;
using OMCS.Passive;
using ESPlus.Application.Group;

namespace Chat.Forms
{
    public partial class GroupForm : Form ,IManagedForm<string>
    {
        private object lockObject = new object();
        private string userID;
        private string groupID;
        private RapidPassiveEngine rapidPassiveEngine;
        private IMultimediaManager multimediaManager;

        public GroupForm(string _groupID,RapidPassiveEngine _rapidEngine,IMultimediaManager _multimediaManager)
        {
            InitializeComponent();
            this.userID = _rapidEngine.CurrentUserID;
            this.groupID = _groupID;
            this.rapidPassiveEngine = _rapidEngine;
            this.multimediaManager = _multimediaManager;

            this.groupChatControl1.Initialize(_groupID,_rapidEngine);
            this.Initialize();
        }

        #region IManagedForm<string> 成员

        public string FormID
        {
            get { return this.groupID; }
        }

        #endregion

        private void Initialize()
        {
            //预订组事件
            this.rapidPassiveEngine.GroupOutter.GroupmateConnected += new CbGeneric<string>(OnGroupmateConnected);
            this.rapidPassiveEngine.GroupOutter.GroupmateOffline +=new CbGeneric<string>(OnGroupmateOffline);
            this.rapidPassiveEngine.GroupOutter.BroadcastReceived += new CbGeneric<string, string, int, byte[]>(GroupOutter_BroadcastReceived);

            Groupmates groupmates = this.rapidPassiveEngine.GroupOutter.GetGroupMembers(this.groupID);
            //foreach (string groupmateID in groupmates.OnlineGroupmates)//组的在线成员
            //{
            //    if (groupmateID != this.userID && !this.ContainsGroupmate(groupmateID))
            //    {
            //        this.AddGroupmate(groupmateID);
            //    }
            //}
            foreach (string groupmateID in groupmates.OnlineGroupmates)//组的在线成员
            {
                if (groupmateID != this.userID && !this.ContainsGroupmate(groupmateID))
                {
                    this.AddGroupmate(groupmateID);
                    SetOnLineGrouper(groupmateID, listView1, Color.Red);
                }
            }
            foreach (string groupmateID in groupmates.OfflineGroupmates)//组的不在线成员
            {
                if (groupmateID != this.userID && !this.ContainsGroupmate(groupmateID))
                {
                    this.AddGroupmate(groupmateID);
                }
            }
        }

        public bool ContainsGroupmate(string userID)
        {
            foreach (ListViewItem item in this.listView1.Items)
            {
                if (item.Text == userID)
                {
                    return true;
                }
            }
            return false;
        }

        //listView1 Add
        public void AddGroupmate(string userID)
        {
            lock (this.lockObject)
            {
                if (this.ContainsGroupmate(userID))
                {
                    return;
                }
                this.listView1.Items.Add(userID, 0);
            }
        }

        public void OnGroupmateConnected(string memberID)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CbGeneric<string>(this.OnGroupmateConnected), memberID);
            }
            else
            {
                //if (!this.ContainsGroupmate(memberID))
                //{
                //    this.AddGroupmate(memberID);
                    SetOnLineGrouper(memberID, listView1, Color.Red);
                //}
            }

        }

        public void OnGroupmateOffline(string memberID)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new CbGeneric<string>(this.OnGroupmateOffline), memberID);
            }
            else
            {
                ListViewItem target = null;
                foreach (ListViewItem item in this.listView1.Items)
                {
                    if (item.Text == memberID)
                    {
                        target = item;
                        break;
                    }
                }

                if (target != null)
                {
                    //this.listView1.Items.Remove(target);
                    SetOnLineGrouper(target.Text, listView1, Color.Black);
                }
            }
        }

        public void GroupOutter_BroadcastReceived(string broadcastID, string groupID, int broadcastType, byte[] broadcastContent)
        {
            if (groupID != this.groupID)
            {
                return;
            }

            if (broadcastType == ESFramework.Demos.Core.GroupInformationTypes.GroupChat)
            {
                ESFramework.Demos.Core.TextChatContract contract = (ESFramework.Demos.Core.TextChatContract)ESBasic.Helpers.SerializeHelper.DeserializeBytes(broadcastContent, 0, broadcastContent.Length);
                //this.groupChatControl1.ShowOtherTextChat(broadcastID,contract);
                this.groupChatControl1.ShowOtherTextChat(broadcastID, contract);
            }
        }

        #region SetOnLineFriend
        private void SetOnLineGrouper(List<string> list, ListView listview, Color color)
        {
            foreach (string ID in list)
            {
                for (int i = 0; i < listview.Items.Count; i++)
                {
                    if (listview.Items[i].Text == ID)
                    {
                        listview.Items[i].ForeColor = color;
                    }
                }
            }
        }
        #endregion

        #region SetOnLineFriend
        private void SetOnLineGrouper(string GrouperID, ListView listview, Color color)
        {
            for (int i = 0; i < listview.Items.Count; i++)
            {
                if (listview.Items[i].Text == GrouperID)
                {
                    listview.Items[i].ForeColor = color;
                }
            }
        }
        #endregion
    }
}
